package com.zlf.sb.demo.completableFuture.bingxing;

import com.alibaba.fastjson.JSON;
import com.zlf.sb.demo.util.SmallTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CompletableFutureReturn {

    public static void main(String[] args) {
        Map<Object, Object> resultMap = new HashMap<>();
        SmallTool.printTimeAndThread("开始");
        CompletableFuture<Map<String, Object>> completableFutureOfYwcjSbCount = CompletableFuture.supplyAsync(
                () ->  getzcywcjBysbCount())
                .whenComplete((result, throwable) -> {
                    //任务完成时执行,用resultMap存放任务的返回值。
                    if (result != null) {
                        resultMap.put("ywcjSbCount", result);
                    }
                    //触发异常
                    if (throwable != null) {
                        log.error("completableFutureOfYwcjSbCount[whenComplete] error:{}", throwable);
                    }
                });
        CompletableFuture<Map<String,Object>> completableFutureOfLxfx = CompletableFuture.supplyAsync(
                () ->  selectLxfx())
                .whenComplete((result, throwable) -> {
                    if (result != null) {
                        resultMap.put("lxfx", result);
                    }
                    //触发异常
                    if (throwable != null) {
                        log.error("completableFutureOfLxfx[whenComplete] error:{}", throwable);
                    }
                });
        CompletableFuture<List> completableFutureOfSbNumBySblx = CompletableFuture.supplyAsync(
                () ->  getSbNumBySblx())
                .whenComplete((result, throwable) -> {
                    if (result != null) {
                        resultMap.put("sbNumBySblx", result);
                    }
                    //触发异常
                    if (throwable != null) {
                        log.error("completableFutureOfSbNumBySblx[whenComplete] error:{}", throwable);
                    }
                });
        /*CompletableFuture<List<DzYwcjVo>> completableFutureOfFbqk = CompletableFuture.supplyAsync(
                () -> dzYwcjService.selectFbqk(), threadPoolExecutor)
                .whenComplete((result, throwable) -> {
                    if (result != null) {
                        resultMap.put("fbqk", result);
                    }
                    //触发异常
                    if (throwable != null) {
                        log.error("completableFutureOfFbqk[whenComplete] error:{}", throwable);
                    }
                });
        CompletableFuture<List<DzYwcjVo>> completableFutureOfFrequentSearch = CompletableFuture.supplyAsync(
                () -> dzYwcjService.frequentSearch(), threadPoolExecutor)
                .whenComplete((result, throwable) -> {
                    if (result != null) {
                        resultMap.put("frequentSearch", result);
                    }
                    //触发异常
                    if (throwable != null) {
                        log.error("completableFutureOfFrequentSearch[whenComplete] error:{}", throwable);
                    }
                });*/

        try  {
            //将多个任务，汇总成一个任务，总共耗时不超时2秒
            System.out.println("JSON.toJSONString(resultMap) = " + JSON.toJSONString(resultMap.get("sbNumBySblx")));

            CompletableFuture.allOf(completableFutureOfYwcjSbCount, completableFutureOfLxfx, completableFutureOfSbNumBySblx).get();
            System.out.println("JSON.toJSONString(resultMap) = " + JSON.toJSONString(resultMap.get("sbNumBySblx")));
            SmallTool.printTimeAndThread("结束");
        } catch (Exception e) {
            log.error("CompletableFuture[allOf] error:{}", e);
        }


    }

    private static Map<String, Object> getzcywcjBysbCount() {
        Map<String, Object> map=new HashMap<>();
     //   int a=1/0;
        map.put("user",Arrays.asList("111"));
        return map;
    }

    private static Map<String, Object> selectLxfx() {
        Map<String, Object> map=new HashMap<>();
        map.put("user",Arrays.asList("111"));
        return map;
    }

    private static List getSbNumBySblx() {

        return   Arrays.asList("11188");
    }
}
